package com.a0soft.gphone.aTruffleHog.util;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import java.lang.reflect.Method;
import java.util.Properties;

public class aSystemProperties {

	private static boolean	m_bInit;
	private static Class<?>	m_cls;
	private static Method	m_get, m_get2, m_getInt, m_getLong, m_getBoolean;
	
	private static String LOG_TAG = aSystemProperties.class.getSimpleName();

	//////////////////////////////////////////
	public static String 
	Get(String key) 
	{
		_Init();
		
		if (m_get == null) return null;
		
		try {
	        return (String)m_get.invoke(null, key);
        }
        catch (Exception e) {
        	if (aDebug.ENABLED)
				aDebug.e(LOG_TAG, "Get(String)", e);
        }
        return null;
	}
	
	public static String 
	Get(String key, String def) 
	{
		_Init();
		
		if (m_get2 == null) return def;
		
		try {
	        return (String)m_get2.invoke(null, key, def);
        }
        catch (Exception e) {
        	if (aDebug.ENABLED)
				aDebug.e(LOG_TAG, "Get(String, String)", e);
        }
        return def;
	}
	
	public static int 
	GetInt(String key, int def) 
	{
		_Init();
		
		if (m_getInt == null) return def;
		
		try {
	        return (Integer)m_getInt.invoke(null, key, def);
        }
        catch (Exception e) {
        	if (aDebug.ENABLED)
				aDebug.e(LOG_TAG, "GetInt()", e);
        }
        return def;
	}
	
	public static long 
	GetLong(String key, long def) 
	{
		_Init();
		
		if (m_getLong == null) return def;
		
		try {
	        return (Long)m_getLong.invoke(null, key, def);
        }
        catch (Exception e) {
        	if (aDebug.ENABLED)
				aDebug.e(LOG_TAG, "GetLong()", e);
        }
        return def;
	}
	
	public static boolean 
	GetBoolean(String key, boolean def) 
	{
		_Init();
		
		if (m_getBoolean == null) return def;
		
		try {
	        return (Boolean)m_getBoolean.invoke(null, key, def);
        }
        catch (Exception e) {
        	if (aDebug.ENABLED)
				aDebug.e(LOG_TAG, "GetBoolean()", e);
        }
        return def;
	}
	
	public static String
	GetJavaSysProp(String strKey)
	{
		Properties props = System.getProperties();
		Object obj = props.get(strKey);
		if (obj == null) return null;
		return obj.toString();
	}
	
	private static void
	_Init()
	{
		if (m_bInit) return;

		m_bInit = true;
            
		try {
			m_cls = Class.forName("android.os.SystemProperties");
			
			try {
				m_get = m_cls.getMethod("get", String.class);
				m_get.setAccessible(true);
			} catch (Exception e) {
				if (aDebug.ENABLED)
					aDebug.d(LOG_TAG, "no get(String)", e);
            }
			
			try {
				m_get2 = m_cls.getMethod("get", String.class, String.class);
				m_get2.setAccessible(true);
			} catch (Exception e) {
				if (aDebug.ENABLED)
					aDebug.d(LOG_TAG, "no get(String, String)", e);
            }
			
			try {
				m_getInt = m_cls.getMethod("getInt", String.class, int.class);
				m_getInt.setAccessible(true);
			} catch (Exception e) {
				if (aDebug.ENABLED)
					aDebug.d(LOG_TAG, "no getInt()", e);
            }
			
			try {
				m_getLong = m_cls.getMethod("getLong", String.class, long.class);
				m_getLong.setAccessible(true);
			} catch (Exception e) {
				if (aDebug.ENABLED)
					aDebug.d(LOG_TAG, "no getLong()", e);
            }
			
			try {
				m_getBoolean = m_cls.getMethod("getBoolean", String.class, boolean.class);
				m_getBoolean.setAccessible(true);
			} catch (Exception e) {
				if (aDebug.ENABLED)
					aDebug.d(LOG_TAG, "no getBoolean()", e);
            }
        }
        catch (ClassNotFoundException e) {
        	if (aDebug.ENABLED)
				aDebug.d(LOG_TAG, "no android.os.SystemProperties class", e);
        	m_cls = null;
        }
	}
}
